/*
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 */

package spidex.table;

import java.util.List;
import javax.swing.RowFilter;
import javax.swing.RowFilter.Entry;
import spidex.DVDInfo;
import spidex.importer.ColumnType;

/**
 *
 * @author xatrix
 */
public class ColumntTypeRowFilter<DefaultTableModel, Object>  extends RowFilter<DefaultTableModel, Object>{
    private String value;
    private List<DVDInfo> result;
    private ColumnType type;

    public ColumntTypeRowFilter(
            ColumnType type, String value, List<DVDInfo> result ) {
        this.type = type;
        this.value = value;
        this.result = result;
    }

    @Override
    public boolean include(
            Entry<? extends DefaultTableModel, ? extends Object> entry) {
        int identifier = (Integer) entry.getIdentifier();
        DefaultTableModel tableModel = entry.getModel();
        String itemString;
        Integer rating;
        
        //Determin proper type, if string check containing otherwise compare int
        switch(type.getInt()) {
            case 1:
                itemString = result.get(identifier).getTitle();
                break;
            case 2:                
                itemString = result.get(identifier).getCompilation();
                break;
            case 3:
                try {
                    rating = Integer.parseInt( value );
                }
                catch(NumberFormatException ex) {
                    rating = -1;
                }
                
                if( result.get(identifier).getRating() == rating) {
                    return true;
                }
                else {
                    return false;
                }
            case 4:
                itemString = result.get(identifier).getRentedToWho();
                break;
            case 5:
                itemString = result.get(identifier).getBorrowedFromWho();
                break;                
            case 6:
                itemString = result.get(identifier).getInfo();
                break;                
            case 7:
                itemString = result.get(identifier).getGenre();
                break;                
            default:
                return result.get(identifier).getTitle().startsWith(value);
        }
        if(itemString == null) {
            return false;
        }
        else {
            return itemString.toLowerCase().contains(value.toLowerCase());
        }
    }

}
